<template>
	<view>
		<view class="list" @click="openMessage">
			<view class="left">头像</view>
			<image :src="info.avatarJson" mode="" class="img" @click.stop="clickImg()"></image>
			<image class="icon" src="@/static/icon/set-code/nav-back.png" mode=""></image>
		</view>
		<view class="list" @click="changeName">
			<view class="left">昵称</view>
			<view class="pet">{{info.nick}}</view>
			<image class="icon" src="@/static/icon/set-code/nav-back.png" mode=""></image>
		</view>
		<!--  <view class="list" @click="changeAddress">
            <view class="left">服务出发地址</view>
            <view class="pet">{{(departAddress || '暂无') | addressFilter}}</view>
            <image class="icon" src="@/static/icon/set-code/nav-back.png" mode=""></image>
        </view> -->

		<!-- 修改头像-->
		<!-- <uni-popup ref="popup" type="bottom"> -->
		<!-- </uni-popup> -->
		<uni-popup ref="message" type="center">
			<view class="permission-bc" style="width: 540rpx;height:676rpx">
				<view style="height: 73%;"></view>
				<view @click="img" class="d-center" style="height: 78rpx;color: #fff;font-size: 32rpx;">马上开启</view>
				<view @click="closeMessage" class="d-center" style="height: 78rpx;font-size: 28rpx;color: #999999;">以后再说
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import permision from '@/common/js/permission.js'
	import {
		selectNurseInfoCheckById,
		updateAddressById,
	} from '@/apis/certificate/certification.js'

	import Kspcropper from '@/components/uni-ui/ksp-cropper/ksp-cropper.vue'
	import uploadImage from '@/common/js/ossutil/uploadFile.js'
	import uniPopup from '@/components/uni-ui/uni-popup/uni-popup.vue'
	import {
		updateNurses
	} from '@/apis/nurse/index.js'
	import {
		translateapp
	} from '../../../common/js/common.js'
	export default {
		components: {
			uniPopup,
		},
		data() {
			return {
				loc: -1,
				url: '',
				path: '',
				info: '',
				avatarJson: '',
				bc: '',
			}
		},
		onLoad() {
			this.getMision()
			this.info = this.$store.state.userInfo
			this.getMyAddress()
		},
		computed: {
			departAddress() {
				return this.$store.state.myaddress
			},
			departAddress2() {
				return this.$store.state.myaddress2
			},
		},
		onShow() {
			this.getMision()
			uni.$once('myAddress', (res) => {
				let _this = this
				console.log(res, '---===========000000009999999999991========')
				this.$store.commit('setMyAddress', res.my)
				updateAddressById({
					nurseId: this.info.id,
					departAddress: res.my,
					departAddressCity: res.city,
				}).then((res) => {
					console.log('更新成功')
					// plus.nativeUI.toast('更新成功!')
				})
			})
		},
		filters: {
			addressFilter(val) {
				return val.length > 15 ? val.slice(0, 15) + '...' : val
			},
		},
		methods: {
			async getMision() {
				if (uni.getSystemInfoSync().platform == 'android') {
					// 安卓权限查询
					this.loc = await permision.checkAndroidSelfPermission(
						'android.permission.WRITE_EXTERNAL_STORAGE'
					)
					console.log('anzhuo', this.loc)
				}
			},
			openMessage() {
				let that = this
				if (this.loc == -1) {
					that.$refs.message.open()
				} else {
					that.img()
				}
			},
			closeMessage() {
				this.$refs.message.close()
			},
			async getMyAddress() {
				let res = await selectNurseInfoCheckById({
					id: this.info.id,
				})
				this.$store.commit('setMyAddress', res.object.departAddress)
			},

			applyRefund() {
				this.$refs.popup.open()
			},
			// img(){
			// 	this.$refs.popup.open()

			// },
			changeName() {
				uni.navigateTo({
					url: '../change-name/change-name',
				})
			},
			changeAddress() {
				uni.navigateTo({
					url: '../../address/select-address/get-address?type=myAddress',
				})
			},
			img() {
				let _this = this
				if (_this.loc == -1) {
					this.closeMessage()
				}
				let userInfo = this.$store.state.userInfo
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed', 'original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						let tempFiles = res.tempFilePaths[0]
						translateapp(tempFiles, 80, (imgUrl) => {
							console.log(imgUrl, '压缩后的图片---')
							uploadImage(
								imgUrl,
								'nurse/',
								async (result) => {
										userInfo.avatarJson = result
										let res = await updateNurses({
											avatarJson: userInfo.avatarJson,
										})
										// console.log(res,'更新用户信息')
										_this.$store.commit('setUserInfo', userInfo)
										plus.nativeUI.toast('修改成功!')
										// uni.showToast({
										// 	icon:'none',
										// 	title:'修改成功！'
										// })
										setTimeout(() => {
											uni.navigateBack({
												delta: 1,
											})
										}, 300)
									},
									(error) => {
										plus.nativeUI.toast('上传失败!')
										// uni.showToast({
										// 	icon:'none',
										// 	title:'上传失败',
										// })
									}
							)
						})
					},
				})
			},
			//头像预览
			clickImg() {
				console.log(111111111111)
				let _this = this
				uni.previewImage({
					urls: [_this.info.avatarJson],
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {
						console.log(res)
					},
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.cropper-wrap {
		// flex: 1;
		height: 50%;
	}

	.ctrls-wrap {
		display: flex;
		color: white;
		font-size: 34rpx;
		text-align: center;
		line-height: 96rpx;
		border-bottom: 1px solid #fff;
		height: 96rpx;

		.ctrl {
			flex: 1;
			flex-basis: 0;
		}
	}

	.list {
		width: 698rpx;
		height: 140rpx;
		background: rgba(216, 216, 216, 0);
		margin: 0 auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20rpx;
		position: relative;
	}

	.img {
		position: absolute;
		width: 96rpx;
		height: 96rpx;
		right: 84rpx;
		top: 22rpx;
		border-radius: 50%;
		background: url('../../../static/img/mine/header.png');
		background-size: 96rpx;
	}

	.icon {
		width: 48rpx;
		height: 48rpx;
	}

	.pet {
		position: absolute;
		right: 84rpx;
		top: 52rpx;
	}

	.box {
		width: 698rpx;
		height: 283rpx;
		background: rgba(249, 249, 249, 0.78);
		border-radius: 25rpx;
		backdrop-filter: blur(18rpx);
		margin: 0 auto;
		margin-bottom: 34rpx;
		text-align: center;
		font-family: SFProText-Semibold, SFProText;
		font-weight: 600;
		color: rgba(0, 122, 255, 1);

		.title {
			height: 80rpx;
			font-size: 23rpx;
			color: rgba(17, 17, 17, 0.5);
			border-bottom: 2rpx solid rgba(222, 222, 222, 0.5);
			line-height: 80rpx;
			box-sizing: border-box;
		}

		.photo {
			height: 100rpx;
			box-sizing: border-box;
			line-height: 100rpx;
		}
	}

	.permission-bc {
		background-image: url('http://loto.oss-cn-shanghai.aliyuncs.com/email/5b089870931e40cd.png');
		background-size: 100%;
		background-repeat: no-repeat;
	}
</style>